//microcommand define jbo-etym
signature("<valsi>")
inject({ optional: ["lnnbotSideloadFonts"] })
action(async ({ session }, valsi) => {
let t = ""
try { t = await ctx.http.get(("https://vudrux.site/sandbox/miniProxy.php?https://en.wiktionary.org/w/index.php?action=raw&title=Appendix:Lojban/" + valsi), { responseType: "text" }) } catch (e) { return String(e) }
const etymArgs = t.match(/\{\{\s*jbo-etym\s*\|([\s\S]*?)}}/)?.[1]
if (typeof etymArgs !== "string") return [valsi + ": ", ...h.toElementArray(t.match(/===\s*Etymology\s*===(.*?)(?=$|\n=)/s)?.[1].replace(/\{\{\s*der(?:ived)?\s*\|([\s\S]*?)}}/g, (_,a)=>{const p=parseWikiTemplateArgs(a);return "["+p[2]+"] \u2068"+p[3]+"\u2069"}).replace(/\{\{\s*(?:l|link|m|mention)?\s*\|([\s\S]*?)}}/g, (_,a)=>{const p=parseWikiTemplateArgs(a);return (p[3]?"\u2068"+p[3]+"\u2069":"["+p[1]+"] \u2068"+p[2]+"\u2069")+(p[4]||p.t?" ("+(p[4]||p.t)+")":"")}).replace(/\{\{[^|]+\|\s*([^|}]+)\s*\}\}/sg, "$1").trim() || session.i18n(".missing"))]
const etymObj = parseWikiTemplateArgs(etymArgs)
const etym = []
for (const k of ["zh", "en", "hi", "es", "ru", "ar"]) {
    if (!etymObj[k] && !etymObj[`${k}_t`]) continue
    let term = etymObj[`${k}_t`] || "?"
    if (k === "zh" && etymObj.hasOwnProperty("zh_s"))
      term = `<span lang="zh-Hant-TW">${term}</span> / ${etymObj.zh_s}`
    etym.push([k, term, etymObj[k].replace(/'''(.*?)'''/g,"<b>$1</b>")])
}
return `<html><style>${await ctx.lnnbotSideloadFonts?.getDeclarations(['Noto Sans','Noto Sans Devanagari','Noto Sans Arabic','Noto Sans CJK SC']) || ""}</style><table style="font-family:Noto Sans,Noto Sans Devanagari,Noto Sans Arabic,Noto Sans CJK SC;text-align:center" border="1"><tr><th colspan="3">${valsi}</th></tr>${etym.map(([lang, term, phon]) => `<tr><th>${lang}</th><td lang="${lang}" dir="${lang === "ar" ? "rtl" : "ltr"}">${term.replace(/\[\[(?:[^\n\]]+?\|)?(.+?)\]\]/g,"$1")}</td><td>${phon}</td></tr>`).join("")}</table></html>`
})
function parseWikiTemplateArgs(args) {
  let n = 1
  return Object.fromEntries(
    args.split("|").map(arg => {
      const ep = arg.indexOf("=")
      if (ep === -1) return [n++, arg]
      return [arg.slice(0, ep).trim(), arg.slice(ep + 1).trim()]
    })
  )
}
locale("zh-CN", {
  description: "gismu 词源查询",
  usage: "输入格式：单个逻辑语根词（gismu）\n数据实时获取自 Wiktionary",
  messages: {
    missing: "词条中未找到词源信息。",
  },
})